<?php

$url = trim($_GET['url']);
$urls = parse_url($url);
extract($urls, EXTR_OVERWRITE, 'url_');

$domain = $url_scheme.'://'.$url_host;
$preurl = dirname($url);
$buffer = file_get_contents($url);
$buffer = compress($buffer);
$images = array();

preg_match_all('/url\(([^)]*)\)/isU', $buffer, $m);
foreach($m[1] as $img){
	$img = trim($img, '"\'');
	if(preg_match('/^http/', $img)){
		$images[] = $img;
	}
	else if(preg_match('/^\//', $img)){
		$images[] = $domain.$img;
	}
	else{
		$images[] = $preurl.'/'.$img;
	}
}
?>

<h2>Get image url from css</h2>
<form action="" method="get">
	Css URL:
	<input type="text" name="url" size="50" value="<?=htmlspecialchars($url);?>" />
	<input type="submit" />
</form>

<?php
echo '<div style="width:500px;height:400px;overflow:auto;border:1px #333 solid;">';
$images = array_unique($images);
foreach($images as $img){
	printf('<a href="%1$s" target="_blank">%1$s</a> <img src="%1$s" />', $img);
	echo "<hr size=1 />\n";
}
echo '</div>';

function compress($buffer) {
  /* remove comments */
  $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
  /* remove tabs, spaces, newlines, etc. */
  $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
  return $buffer;
}

?>
